﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Collections;
using VehicleManagement.Models;
using VehicleManagement.Codes;
using VehicleManagement.ControllersServices;
using IBatisNet.DataMapper.Configuration;
using IBatisNet.DataMapper;
namespace VehicleManagement.Controllers
{
    //[CustomerAuthorize(Roles = "负责人")]
    public class DriverController : Controller
    {
        const int PAGE_MAX = 5;
        //
        // GET: /Driver/
        DriverServices driverService = new DriverServices();
        public ActionResult Index()
        {

            Hashtable ht = new Hashtable();
            SearchDriverByRequeast(ref ht);
            int page;
            if (!string.IsNullOrWhiteSpace(Request.Params["Page"]))
                page = int.Parse(Request.Params["Page"]);
            else
                page = 1;
            return View(driverService.SearchAllDriver(ht, (page - 1) * PAGE_MAX, PAGE_MAX));

            //return View(driverService.GetAllDriver());//返回所有司机的简要简要信息列表的视图

        }

        /// <summary>
        /// 添加司机
        /// </summary>
        /// <returns></returns>
        public ActionResult Create()
        {
            return View();

        }
        [HttpPost]
        public ActionResult Create(DriverInfo driver)
        {
            if (ModelState.IsValid)
            {
                object obj = driverService.CreateDriver(driver);
                return RedirectToAction("Index");
            }
            else
                return View(driver);
        }

        /// <summary>
        /// 根据司机ID，查看司机详细信息，并返回视图
        /// </summary>
        /// <param name="id">司机的ID</param>
        /// <returns>司机信息详细视图</returns>
        public ActionResult Details(int id)
        {
            return View(driverService.GetDriverByDriverID(id));
        }


        /// <summary>
        /// 司机信息修改
        /// </summary>
        /// <param name="id">司机ID</param>
        /// <param name="driveriofo">返回司机类</param>
        /// <returns>修改司机的视图</returns>
        public ActionResult Edit(int id)
        {
            DriverInfo driverInfo = driverService.GetDriverByDriverID(id);
            return View(driverInfo);
        }
        [HttpPost]
        public ActionResult Edit(DriverInfo driveriofo)
        {
          //  if (ModelState.IsValid)
           // {
                int rows = driverService.EditDriver(driveriofo);
            if(rows==1)
                return RedirectToAction("Index");
           // }
            else
            return View(driveriofo);

        }

        /// <summary>
        /// 根据司机的ID，删除司机
        /// </summary>
        /// <param name="id">司机ID</param>
        /// <returns></returns>
        public ActionResult Delete(int id)
        {
            DriverInfo diverInfo = driverService.GetDriverByDriverID(id);
            return View(diverInfo);
        }

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            if (Request.IsAjaxRequest())
            {
                int i = driverService.DeleteDriver(id);//根据司机ID，删除
                if (i == 1)//删除成功
                {
                    return Json(new { Result = "true", Message = "删除成功" });
                }
                else//删除不成功
                {
                    return Json(new { Result = "false", Message = "删除失败" });
                }
            }
            else
            {
                return Json(new { Result = "false", Message = "该方法只能通过AJAX方式调用" });
            }

        }


        //
        // POST: /Driver/GetDriverNameList
        [HttpPost, ActionName("GetDriverNameList")]
        public ActionResult GetDriverNameList()
        {
            IList<string> list = SqlMapperManager.Instance.Get().QueryForList<string>("SELECT_ALL_DRIVERNAME", null);
            return Json(list);
        }

        //
        // POST: /Driver/GetDriverNameListByState
        [HttpPost, ActionName("GetDriverNameListByState")]
        public ActionResult GetDriverNameListByState(int state)
        {
            IList<string> list = SqlMapperManager.Instance.Get().QueryForList<string>("SELECT_DriverName_BY_Free", state);
            return Json(list);
        }

        [ActionName("GetPagingInfo"),HttpGet]
        public ActionResult GetPagingInfo()
        {
            int currentPage;
            if (string.IsNullOrWhiteSpace(Request.Params["Page"]))
                currentPage = 1;
            else
                currentPage = int.Parse(Request.Params["Page"]);

            Hashtable ht = new Hashtable();
            SearchDriverByRequeast(ref ht);
            int itemCount = driverService.CountAllDrivers(ht);
            return Json(new
            {
                TotalItems = itemCount,
                ItemsPerPage = PAGE_MAX,
                CurrentPage = currentPage,
                TotalPages = (int)Math.Ceiling((decimal)itemCount / PAGE_MAX)
            },JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ht"></param>

        [NonAction]
        private void SearchDriverByRequeast(ref Hashtable ht)
        {

            string driverState = Request.Params["DriverState"];
            string driverName = Request.Params["DriverName"];
            string linceType = Request.Params["LinceType"];
            string driverNameForSearch = "%%";
            string linceTypeForSearch = "%%";
            int driverStateForSearch = 3;

            if (driverState == "是")
                driverStateForSearch = 0;
            else if (driverState == "否")
                driverStateForSearch = 1;

            if (!string.IsNullOrEmpty(driverName) && driverName != "姓名")
                driverNameForSearch = string.Format("%{0}%", driverName);
            if (!string.IsNullOrEmpty(linceType) && linceType != "驾照类型")
                linceTypeForSearch = string.Format("%{0}%", linceType);

            ht.Add("driverNameForSearch", driverNameForSearch);
            ht.Add("driverStateForSearch", driverStateForSearch);
            ht.Add("linceTypeForSearch", linceTypeForSearch);
        }



    }
}
